package com.game.server.db.dao;

import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.util.GetBeanFromResultUtil;

import javax.sql.rowset.CachedRowSet;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

public class XyClubInfoDao {


    /**
     * 查询所有俱乐部
     *
     * @return
     */
    public static HashMap<Integer, XyClubInfo> selectAll() {
        HashMap<Integer, XyClubInfo> mapClub = new HashMap<>();
        String sql = "SELECT * FROM xy_club_info";
        CachedRowSet resultSet = SqlHelper.getResultSet(sql);
        try {
            while (resultSet.next()) {
                XyClubInfo info = GetBeanFromResultUtil.getBean(resultSet, XyClubInfo.class,null);
                mapClub.put(info.getId(), info);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mapClub;
    }


    /**
     * 添加俱乐部
     *
     * @param info
     * @return
     */
    public static int insertInto(XyClubInfo info) {
        int id = DataManager.getInstance().findOnlyID("xy_club_info","id");
        if (id != 0) {
            String sql = "INSERT INTO xy_club_info (id,creatorid,name,headurl,content,createdate) VALUES (?,?,?,?,?,?)";
            Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
            Parameter p2 = new Parameter(Types.INTEGER, info.getCreatorid(), ParameterType.Input);
            Parameter p3 = new Parameter(Types.VARCHAR, info.getName(), ParameterType.Input);
            Parameter p4 = new Parameter(Types.VARCHAR, info.getHeadurl(), ParameterType.Input);
            Parameter p5 = new Parameter(Types.VARCHAR, info.getContent(), ParameterType.Input);
            Parameter p6 = new Parameter(Types.TIMESTAMP, info.getCreatedate(), ParameterType.Input);
            SqlHelper.execute(sql, p1, p2, p3, p4, p5,p6);
        }
        return id;
    }

    public static XyClubInfo findById(int id) {
        XyClubInfo clubInfo = new XyClubInfo();
        String cmdText = "SELECT * FROM xy_club_info WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);

        CachedRowSet resultSet = SqlHelper.getResultSet(cmdText, p1);
        try {
            if (resultSet.next()) {
                clubInfo = GetBeanFromResultUtil.getBean(resultSet, XyClubInfo.class,null);
            }
        } catch (SQLException e) {
            LogManager.getInstance().errorLog(e);
        }
        return clubInfo;
    }

    public static int updateClubByObj(XyClubInfo clubInfo) {
        LogManager.getInstance().Log("name:"+clubInfo.getId());
        LogManager.getInstance().Log("name:"+clubInfo.getName());
        LogManager.getInstance().Log("headurl:"+clubInfo.getName());
        LogManager.getInstance().Log("content:"+clubInfo.getName());
        LogManager.getInstance().Log("xingyuvalue:"+clubInfo.getName());
        String sql = "update xy_club_info set name=?,headurl=?,content=?,xingyuvalue=? where id=?";
        Parameter p1 = new Parameter(Types.VARCHAR, clubInfo.getName(), ParameterType.Input);
        Parameter p2 = new Parameter(Types.VARCHAR, clubInfo.getHeadurl(), ParameterType.Input);
        Parameter p3 = new Parameter(Types.VARCHAR, clubInfo.getContent(), ParameterType.Input);
        Parameter p4 = new Parameter(Types.INTEGER, clubInfo.getXingyuvalue(), ParameterType.Input);
        Parameter p5 = new Parameter(Types.INTEGER, clubInfo.getId(), ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2, p3, p4, p5);
    }

    public static int updatePromotionType(int type,int id) {
        String sql = "update xy_club_info set promotion_type=? where id=?";
        Parameter p1 = new Parameter(Types.INTEGER, type, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }

    public static int updateJiJin(int id, Integer value) {
        String sql = "update xy_club_info set clubjijin=clubjijin+? where id=?";
        Parameter p1 = new Parameter(Types.INTEGER, value, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }

    /**
     * 添加俱乐部用户的 每日贡献  手数
     * @param uid
     * @param id
     * @param type 1 增加贡献 2增加手数
     * @param partnerAcquisition
     */
    public static void addDayClubData(int uid, int id, int type, int partnerAcquisition,int pid) {
        System.out.println("uid添加俱乐部用户的"+uid);
        System.out.println("id添加俱乐部用户的"+id);
        System.out.println("type添加俱乐部用户的"+type);
        System.out.println("partnerAcquisition添加俱乐部用户的"+partnerAcquisition);
        System.out.println("pid添加俱乐部用户的"+pid);
        if(type ==1){
            String sql = "INSERT INTO xy_club_user_daydata (pid,uid,cid,day_shoushu,day_contribute,creationdate) VALUES (?,?,?,?,?,?)";

            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date1=formatter.format(date);
            Parameter p1 = new Parameter(Types.INTEGER, pid, ParameterType.Input);
            Parameter p2 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
            Parameter p3 = new Parameter(Types.INTEGER, id, ParameterType.Input);
            Parameter p4 = new Parameter(Types.INTEGER, 0, ParameterType.Input);
            Parameter p5 = new Parameter(Types.INTEGER, partnerAcquisition, ParameterType.Input);
            Parameter p6 = new Parameter(Types.TIMESTAMP, Timestamp.valueOf(date1), ParameterType.Input);
            SqlHelper.execute(sql, p1, p2, p3, p4, p5,p6);
        } else {
            String sql = "INSERT INTO xy_club_user_daydata (pid,uid,cid,day_shoushu,day_contribute,creationdate) VALUES (?,?,?,?,?,?)";

            Date date = new Date();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String date1=formatter.format(date);
            Parameter p1 = new Parameter(Types.INTEGER, pid, ParameterType.Input);
            Parameter p2 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
            Parameter p3 = new Parameter(Types.INTEGER, id, ParameterType.Input);
            Parameter p4 = new Parameter(Types.INTEGER, partnerAcquisition, ParameterType.Input);
            Parameter p5 = new Parameter(Types.INTEGER, 0, ParameterType.Input);
            Parameter p6 = new Parameter(Types.TIMESTAMP, Timestamp.valueOf(date1), ParameterType.Input);
            SqlHelper.execute(sql, p1, p2, p3, p4, p5,p6);
        }

    }
    

    public static int addUnionedu(int id, Integer value) {
        String sql = "UPDATE xy_club_info SET unionedu=unionedu+? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, value, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
        return SqlHelper.execute(sql, p1, p2);
    }

    public static void updateUnionId(int id, int unionid) {
        String sql = "UPDATE xy_club_info SET unionid=? WHERE id = ?";
        Parameter p1 = new Parameter(Types.INTEGER, unionid, ParameterType.Input);
        Parameter p2 = new Parameter(Types.INTEGER, id, ParameterType.Input);
        SqlHelper.execute(sql, p1, p2);
    }

    public static void del(int id) {
        String sql = "delete from xy_club_changes_quota where cid = ?";
        String sql2 = "delete from xy_club_contribution_log where cid = ?";
        String sql3 = "delete from xy_club_info where id = ?";
        String sql4 = "delete from xy_club_jijin_log where cid = ?";
        String sql5 = "delete from xy_club_member_info where cid = ?";
        String sql6 = "delete from xy_club_premium_oplog where cid = ?";
        String sql7 = "delete from xy_club_user_daydata where cid = ?";
        String sql8 = "delete from xy_allgeme_log where cid = ?";
        String sql9 = "delete from xy_mingeme_log where cid = ?";
        String sql10 = "delete from xy_notification_log where notification_type = 1 and (cid = ? or uid = ?)";
        Parameter p1 = new Parameter(Types.INTEGER, id, ParameterType.Input);
        SqlHelper.execute(sql,p1);
        SqlHelper.execute(sql2,p1);
        SqlHelper.execute(sql3,p1);
        SqlHelper.execute(sql4,p1);
        SqlHelper.execute(sql5,p1);
        SqlHelper.execute(sql6,p1);
        SqlHelper.execute(sql7,p1);
        SqlHelper.execute(sql8,p1);
        SqlHelper.execute(sql9,p1);
        SqlHelper.execute(sql10,p1,p1);
    }
}
